package handlers

import (
	"data_service/models"
	"data_service/server"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// CreateLogRequest 定义创建日志请求结构
type CreateLogRequest struct {
	UserID        int    `json:"user_id"`
	OperationType string `json:"operation_type" binding:"required"`
	Content       string `json:"content" binding:"required"`
	IPAddress     string `json:"ip_address"`
	UserAgent     string `json:"user_agent"`
	Status        string `json:"status"`
}

// 初始化日志业务服务
var logService = server.NewLogService()

// CreateLog 创建日志记录
func CreateLog(c *gin.Context) {
	var req CreateLogRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求参数"})
		return
	}

	log := &models.Log{
		UserID:        req.UserID,
		OperationType: req.OperationType,
		Content:       req.Content,
		IPAddress:     req.IPAddress,
		UserAgent:     req.UserAgent,
		Status:        req.Status,
	}

	if err := logService.CreateLog(log); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建日志失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "日志创建成功", "log_id": log.ID})
}

// GetUserLogs 获取用户操作日志
func GetUserLogs(c *gin.Context) {
	userID := c.Param("user_id")
	startTimeStr := c.Query("start_time")
	endTimeStr := c.Query("end_time")

	// 解析用户ID
	uid, err := strconv.Atoi(userID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的用户ID"})
		return
	}

	// 解析时间范围
	var startTime, endTime time.Time
	if startTimeStr != "" {
		startTime, err = time.Parse(time.RFC3339, startTimeStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "无效的开始时间格式，应为RFC3339格式"})
			return
		}
	} else {
		startTime = time.Now().AddDate(0, 0, -7) // 默认查询最近7天
	}

	if endTimeStr != "" {
		endTime, err = time.Parse(time.RFC3339, endTimeStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "无效的结束时间格式，应为RFC3339格式"})
			return
		}
	} else {
		endTime = time.Now()
	}

	logs, err := logService.GetUserLogsByTimeRange(uid, startTime, endTime)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取日志失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"logs": logs})
}

// GetSystemLogs 获取系统日志
func GetSystemLogs(c *gin.Context) {
	startTimeStr := c.Query("start_time")
	endTimeStr := c.Query("end_time")

	// 解析时间范围
	var startTime, endTime time.Time
	var err error

	if startTimeStr != "" {
		startTime, err = time.Parse(time.RFC3339, startTimeStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "无效的开始时间格式，应为RFC3339格式"})
			return
		}
	} else {
		startTime = time.Now().AddDate(0, 0, -7) // 默认查询最近7天
	}

	if endTimeStr != "" {
		endTime, err = time.Parse(time.RFC3339, endTimeStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "无效的结束时间格式，应为RFC3339格式"})
			return
		}
	} else {
		endTime = time.Now()
	}

	logs, err := logService.GetSystemLogsByTimeRange(startTime, endTime)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取系统日志失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"logs": logs})
}
